<?php
class Common{
	/**
	 * 获得随机字符串
	 * @param int $defaultLen
	 * @param string $seed
	 */
	public static function getBarCode($length=32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'){
		$hash = '';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++)	{
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}
	function random($length=32, $chars = 'ABCDE012FGHMnopqrstNOPQghijuvwxyRSTUVklmWXYZ34567IJKL89abcdefz') {
		$hash = '';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++)	{
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}
	public static function getCode($type=true,$length=32,$char='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'){
		//return strtoupper(Common::getBarCode(32,$char));
		$hash = '';
		$max = strlen($char) - 1;
		for($i = 0; $i < $max; $i++)	{
			$hash .= $char[mt_rand(0, $max)];
		}if ($type){
			return strtoupper(md5(microtime().$hash));
		}else{
			return md5(microtime().$hash);
		}
	}
	/**
	 * 转16进制
	 * Enter description here ...
	 * @param unknown_type $str
	 */
	public static function escape($str) {
		preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/", $str, $r);
		$ar = $r[0];
		foreach ($ar as $k => $v) {
			if (ord($v[0]) < 128)
				@$ar[$k] = rawurlencode($v);
			else
				@$ar[$k] = "%u" . bin2hex(iconv("GB2312", "UCS-2", $v));
		}
		return join("", $ar);
	}
	/**
	 * 转16进制
	 * Enter description here ...
	 * @param unknown_type $string
	 * @param unknown_type $encoding
	 */
	public static function escapeToHex($string, $encoding = 'UTF-8') {
		$return = '';
		for ($x = 0; $x < mb_strlen($string, $encoding); $x++) {
			$str = mb_substr($string, $x, 1, $encoding);
			if (strlen($str) > 1) {
				$return .= '%u' . strtoupper(bin2hex(mb_convert_encoding($str, 'UCS-2', $encoding)));
			} else {
				$return .= '%' . strtoupper(bin2hex($str));
			}
		}
		return $return;
	}
	/**
	 * unicode编码
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	public static function unicode_encode($name) {
		$name = iconv('UTF-8', 'UCS-2', $name);
		$len = strlen($name);
		$str = '';
		for ($i = 0; $i < $len -1; $i = $i +2) {
			$c = $name[$i];
			$c2 = $name[$i +1];
			if (ord($c) > 0) {
				$str .= '\u' . base_convert(ord($c), 10, 16) . base_convert(ord($c2), 10, 16);
			} else {
				$str .= $c2;
			}
		}
		return $str;
	}
 
	/**
	 * 中文字符串截取
	 * @param string $str
	 * @param int $start 开始截取位置
	 * @param int $length 截取长度，默认为余下全部
	 * @param string $charset 字符编码
	 * @param bool $suffix 是否在返回结果后面加 '…'
	 */
	public static function cn_substr($str, $start = 0, $length=null, $charset = "utf-8", $suffix = true) {
		
		if (empty($length))$length=strlen($str)-$start;
		
		if (function_exists("mb_substr")) {
			if (mb_strlen($str, $charset) <= $length)
				return $str;
			$slice = mb_substr($str, $start, $length, $charset);
		} else {
			$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
			$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
			$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
			$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
			preg_match_all($re[$charset], $str, $match);
			if (count($match[0]) <= $length)
				return $str;
			$slice = join("", array_slice($match[0], $start, $length));
		}
		if ($suffix)
			return $slice . "…";
		return $slice;
	}
}